package com.ledim.widget.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.letv.android.young.client.R;

public class XListViewFooter extends LinearLayout {

    public final static int STATE_NORMAL = 0;
    public final static int STATE_READY = 1;
    public final static int STATE_LOADING = 2;
    public final static int STATE_OVER = 3;

    private Context mContext;

    private View mContentView;
    private ImageView mProgressBar;
    private TextView mHintView;
    private AnimationDrawable mLoadAnimationDrawable = null;

    public XListViewFooter(Context context) {
        super(context);
        initView(context);
    }

    public XListViewFooter(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public void setState(int state) {
        mHintView.setVisibility(View.VISIBLE);
        //		mProgressBar.setVisibility(View.INVISIBLE);
        mHintView.setTextColor(Color.parseColor("#333333"));

        if (state == STATE_READY) {
            mHintView.setVisibility(View.VISIBLE);
            mHintView.setText(R.string.xlistview_footer_hint_ready);
            mProgressBar.setVisibility(View.GONE);
        } else if (state == STATE_LOADING) {
            //			mProgressBar.setVisibility(View.VISIBLE);
            mHintView.setText("大波内容正在赶来…");
            mProgressBar.setVisibility(View.VISIBLE);
        } else if (state == STATE_OVER) {
            mHintView.setVisibility(View.VISIBLE);
            //			mHintView.setText(R.string.xlistview_footer_hint_over);
            mHintView.setText("还不满足嘛，小妖精~");
            mHintView.setTextColor(Color.parseColor("#888888"));
            mProgressBar.setVisibility(View.GONE);
        } else {
            mHintView.setVisibility(View.VISIBLE);
            mHintView.setText(R.string.xlistview_footer_hint_normal);
            mProgressBar.setVisibility(View.GONE);
        }
    }

    public void setBottomMargin(int height) {
        if (height < 0) {
            return;
        }
        LayoutParams lp = (LayoutParams) mContentView.getLayoutParams();
        lp.bottomMargin = height;
        mContentView.setLayoutParams(lp);
    }

    public int getBottomMargin() {
        LayoutParams lp = (LayoutParams) mContentView.getLayoutParams();
        return lp.bottomMargin;
    }

    /**
     * normal status
     */
    public void normal() {
        mHintView.setText("查看更多");
        //		mProgressBar.setVisibility(View.GONE);
    }

    /**
     * loading status
     */
    public void loading() {
        mHintView.setText("加载中...");
        //		mProgressBar.setVisibility(View.VISIBLE);
    }

    /**
     * hide footer when disable pull load more
     */
    public void hide() {
        LayoutParams lp = (LayoutParams) mContentView.getLayoutParams();
        lp.height = 0;
        mContentView.setLayoutParams(lp);
    }

    /**
     * show footer
     */
    public void show() {
        LayoutParams lp = (LayoutParams) mContentView.getLayoutParams();
        lp.height = LayoutParams.WRAP_CONTENT;
        mContentView.setLayoutParams(lp);
    }

    LinearLayout moreView;

    private void initView(Context context) {
        mContext = context;
        moreView = (LinearLayout) LayoutInflater.from(mContext)
                                                .inflate(R.layout.xlistview_footer, null);
        addView(moreView);
        moreView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        mContentView = moreView.findViewById(R.id.xlistview_footer_content);
        mProgressBar = (ImageView) moreView.findViewById(R.id.xlistview_footer_progressbar);
        mHintView = (TextView) moreView.findViewById(R.id.xlistview_footer_hint_textview);

        mProgressBar.setImageResource(R.anim.refresh_animation);
        mLoadAnimationDrawable = (AnimationDrawable) mProgressBar.getDrawable();
        mLoadAnimationDrawable.setOneShot(false);
        mLoadAnimationDrawable.start();
    }

    public int getVisiableHeight() {
        return moreView.getHeight();
    }

}
